<template>
  <div>
    <div v-if="show" class="cus">
      <div class="cus_box">
        <div class="title">
          <span>Battery Details</span
          ><i @click="show = false" class="el-icon-close"></i>
        </div>
        <div class="content">
          <ul>
            <li>
              <span>Battery ID:</span><span>{{ battery_info.batteryNo }}</span>
            </li>
            <li>
              <span>Total Voltage：</span
              ><span>{{ (battery_info.voltage / 100).toFixed(2) }}V</span>
            </li>
            <li>
              <span>Battery Status：</span
              ><span>{{
                battery_info.status == 0
                  ? "Charging"
                  : battery_info.status == 1
                  ? "Rent"
                  : battery_info.status == 2
                  ? "Leave Unused"
                  : "--"
              }}</span>
            </li>
            <li>
              <span>Battery Temperature：</span
              ><span>{{ battery_info.temperature }}℃</span>
            </li>
            <li>
              <span>Battery Capacity：</span
              ><span>{{ (battery_info.ah / 100).toFixed(2) }}A</span>
            </li>
            <li>
              <span>Dump Energy：</span
              ><span>{{ battery_info.remainSoc }}%</span>
            </li>
            <li>
              <span>Battery Position：</span><span>{{ currentAddress }}</span>
            </li>
          </ul>
          <div>
            <div
              id="searchResultPanel"
              style="
                border: 1px solid #c0c0c0;
                width: 150px;
                height: auto;
                display: none;
              "
            ></div>
            <div id="l-map"></div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import getAddress from "../../js/map";
import axios from "axios";
export default {
  name: "battery_details",
  data() {
    return {
      baiduMap: null,
      show: false,
      battery_info: {},
      currentAddress: ""
    };
  },
  watch: {
    show: function(state) {
      if (!state) {
        this.currentAddress = "";
      }
    }
  },
  created() {},

  mounted() {
    // console.log(13452222, this.battery_info);
  },
  methods: {
    async map() {
      console.log(13452222, this.battery_info.lng, this.battery_info.lat);
      // 如果经纬度有值
      if (Number(this.battery_info.lng) && Number(this.battery_info.lat)) {
        // 后端传来的经纬度是wgs84国际坐标的，需要把经纬度转为百度的
        axios
          .get(
            `${process.env.VUE_APP_URL}/baidumap/geoconv/v1/?coords=${this.battery_info.lng},${this.battery_info.lat}&from=1&to=5&ak=uQ1kN7e2ILDy4w5LGQ4c14uEd7KK2GGY`,
            {
              headers: {
                token: sessionStorage.token
              }
            }
          )
          .then(async res => {
            console.log("Success", res.data.result[0]);
            let point = res.data.result[0];
            this.currentAddress = await getAddress(
              Number(point.x),
              Number(point.y)
            );
          });
      } else {
        this.currentAddress = "None Battery Position";
      }
    }
  }
};
</script>

<style scoped lang="scss">
.content {
  ul {
    li {
      line-height: 30px;
    }
  }
}

#l-map {
  height: 350px;
  width: 100%;
}
</style>
